/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests;

import Utilities.Constants.VirtualResourceType;
import Utilities.Constants;

/**
 * This class represents a simplified version of <code>AuthorizationRequest</code>.
 * <br><br>
 * This object is used instead of its superclass when the requested action does not
 * deal with virtual resources.
 *
 * @see AuthorizationRequest
 *
 * @author Angel Sanadinov
 */
public class SimpleAuthorizationRequest extends AuthorizationRequest
{
    private boolean isServerIdRequired;

    /**
     * Constructs a new request object using the specified data. <br><br>
     *
     * Used for checking whether a user is allowed to perform an action.
     *
     * <b>Note:</b> <i>Once constructed, the object cannot be modified!</i>
     *
     * @param userId the id of the requestor
     * @param action the id of the action that is being requested
     */
    public SimpleAuthorizationRequest(int userId, int action)
    {
        super(userId, Constants.INVALID_SERVER_ID, action, VirtualResourceType.INVALID, null);
        this.isServerIdRequired = false;
    }

    /**
     * Constructs a new request object using the specified data. <br><br>
     *
     * Used for checking whether an action on a resource type is permitted for
     * the specified user on the specified server.
     *
     * <b>Note:</b> <i>Once constructed, the object cannot be modified!</i>
     *
     * @param userId the id of the requestor
     * @param serverId the id of the server on which the actions it to be carried out
     * @param action the id of the action
     * @param resourceType the type of resource that the action is intended for
     */
    public SimpleAuthorizationRequest(int userId, int serverId, int action, VirtualResourceType resourceType)
    {
        super(userId, serverId, action, resourceType, null);
        this.isServerIdRequired = true;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is valid only when all data supplied when constructing it is
     * valid.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        if(isRequestorIdValid() && isRequestedActionValid() 
           && (!isServerIdRequired || (isServerIdValid() && getAffectedResourceType() != null
                                       && getAffectedResourceType() != VirtualResourceType.INVALID)))
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return SimpleAuthorizationRequest.class.getSimpleName();
    }
}
